package com.fjp.heartguard.ui.question.fragment

import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RadioGroup
import android.widget.ScrollView
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import com.fjp.heartguard.R
import com.fjp.heartguard.app.base.BaseFragment
import com.fjp.heartguard.app.base.HTBaseViewModel
import com.fjp.heartguard.app.data.bean.Questions
import com.fjp.heartguard.app.ext.dp
import com.fjp.heartguard.databinding.FragmentMultipleBinding
import com.zhpan.bannerview.utils.BannerUtils

class MultipleFragment : BaseFragment<HTBaseViewModel, FragmentMultipleBinding>() {
    var mHandler: Handler = Handler()


    companion object {
        fun newInstance(questions: Questions?): Fragment {
            val fragment = MultipleFragment()
            val bundle = Bundle()
            bundle.putSerializable("question", questions)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        val questions = arguments?.getSerializable("question") as Questions?
        questions?.let {
            mDatabind.questionTitleLayout.setType(questions.questionType)
            mDatabind.questionTv.text = it.questionTitle
            it.options?.forEachIndexed { index,options ->
                var cb = View.inflate(context, R.layout.layout_checkbox_button, null) as CheckBox
                cb.setOnCheckedChangeListener { buttonView, isChecked -> options.isSelected = isChecked }
                cb.isChecked = options.isSelected
                mDatabind.optionContentLL.addView(cb)
                val editText = View.inflate(context, R.layout.edit_text, null) as EditText
                if (options.title.startsWith("*")) {
                    mDatabind.optionContentLL.addView(editText)
                    val editTextLayoutParams = editText.layoutParams as LinearLayout.LayoutParams
                    editTextLayoutParams.setMargins(0, 16.dp.toInt(), 0, 0)
                    editTextLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT
                    editText.layoutParams = editTextLayoutParams
                    editText.setText(options.value)
                    if (cb.isChecked) {
                        editText.visibility = View.VISIBLE
                        if(index==it.options.size-1){
                            mHandler.postDelayed({ mDatabind.scrollView.fullScroll(ScrollView.FOCUS_DOWN) }, 100)
                        }
                    } else {
                        editText.visibility = View.GONE
                    }
                    editText.addTextChangedListener {
                        options.value = it.toString()
                    }
                }
                cb.setOnCheckedChangeListener { buttonView, isChecked ->
                    run {
                        options.isSelected = isChecked
                        if (cb.isChecked) {
                            editText.visibility = View.VISIBLE
                            if(index==it.options.size-1){
                                mHandler.postDelayed({ mDatabind.scrollView.fullScroll(ScrollView.FOCUS_DOWN) }, 100)
                            }
                        } else {
                            editText.visibility = View.GONE
                        }
                        if(isChecked && it.questionTitle.startsWith("6、被评估人通常记得的信息（可多选）") && cb.text=="以上均不记得"){
                            it.options.forEachIndexed { index, options ->
                                if(index!=it.options.size-1){
                                    (mDatabind.optionContentLL.getChildAt(index) as CheckBox).isChecked=false
                                    options.isSelected=false
                                }
                            }
                        }else if(isChecked && it.questionTitle.startsWith("6、被评估人通常记得的信息（可多选）") && cb.text!="以上均不记得"){
                            (mDatabind.optionContentLL.getChildAt(4) as CheckBox).isChecked=false
                            it.options[ it.options.size-1].isSelected=false
                        }else if(isChecked && it.questionTitle.startsWith("78、是否被诊断患有以下慢性病") && cb.text=="以上都没有"){
                            it.options.forEachIndexed { index, options ->
                                if(index!=it.options.size-2){
                                    (mDatabind.optionContentLL.getChildAt(index) as CheckBox).isChecked=false
                                    options.isSelected=false
                                }
                            }
                        }else if(isChecked && it.questionTitle.startsWith("78、是否被诊断患有以下慢性病") && cb.text!="以上都没有"){
                            (mDatabind.optionContentLL.getChildAt(10) as CheckBox).isChecked=false
                            it.options[10].isSelected=false
                        }else if(isChecked && it.questionTitle.startsWith("59、在过去一个月内，您服药时是否出现过以下情况？") && cb.text=="以上情况都没有发生"){
                            it.options.forEachIndexed { index, options ->
                                if(index!=it.options.size-1){
                                    (mDatabind.optionContentLL.getChildAt(index) as CheckBox).isChecked=false
                                    options.isSelected=false
                                }
                            }
                        }else if(isChecked && it.questionTitle.startsWith("59、在过去一个月内，您服药时是否出现过以下情况？") && cb.text!="以上情况都没有发生"){
                                it.options[it.options.size-1].isSelected=false
                            (mDatabind.optionContentLL.getChildAt(it.options.size-1) as CheckBox).isChecked=false
                        }
                    }

                }
                val layoutParams = cb.layoutParams as LinearLayout.LayoutParams
                layoutParams.setMargins(0, 16.dp.toInt(), 0, 0)
                layoutParams.width = RadioGroup.LayoutParams.MATCH_PARENT
                cb.layoutParams = layoutParams
                cb.text = options.title.replace("*", "")

            }

        }
    }

}